package com.anrongtec.zcpt.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.model.demo.InstallApps;

import java.util.List;

/**
 * 作者：liuhui on 2017/2/13 19:28
 * 邮箱：liu594545591@126.com
 * 描述：GridViewAdapter
 */

public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private List<InstallApps> mImageData;
    private View.OnClickListener listener;
    private LayoutInflater layoutInflater;

    public GridViewAdapter(Context context, List<InstallApps> mImageData) {
        this.context = context;
        this.mImageData = mImageData;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return mImageData.size() + 1;//注意此处
    }

    @Override
    public Object getItem(int position) {
        return mImageData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_grid_install_app, parent, false);
            viewHolder.mDragAppImageView = (ImageView) convertView.findViewById(R.id.iv_item_drag_app);
            viewHolder.mDragAppTextView = (TextView) convertView.findViewById(R.id.tv_item_drag_app);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position < mImageData.size()) {
            viewHolder.mDragAppImageView.setImageResource(mImageData.get(position).getImgUrl());
            viewHolder.mDragAppTextView.setText(mImageData.get(position).getAppName());
        } else {
            viewHolder.mDragAppImageView.setImageResource(R.drawable.icon_more_app);//最后一个显示加号图片
            viewHolder.mDragAppImageView.setOnClickListener(listener);
        }
        return convertView;
    }

    final class ViewHolder {
        ImageView mDragAppImageView;
        TextView mDragAppTextView;
    }

}

